﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security;
using System.Security.Cryptography;
using System.IO;

namespace Utility
{
    /// <summary>
    /// Security 的摘要说明。
    /// </summary>
    public class SecurityHelper
    {
        /// <summary>
        /// 密码加密方式
        /// </summary>
        public enum Security_EncryptType
        {
            /// <summary>
            /// SHA1加密
            /// </summary>
            SHA1,

            /// <summary>
            /// MD5加密
            /// </summary>
            MD5
        };


        /// <summary>
        /// 加密密码
        /// </summary>
        /// <param name="password">加密前密码</param>
        /// <param name="encryptType">加密方式：SHA1/MD5</param>
        /// <returns>加密后密码</returns>
        public static string ToEncryptPassword(string password, Security_EncryptType encryptType)
        {
            string encryptedPwd = password;  //加密后密码

            switch (encryptType)
            {
                case Security_EncryptType.SHA1:
                    encryptedPwd = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "SHA1");
                    break;
                case Security_EncryptType.MD5:
                    encryptedPwd = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5");
                    break;
                default:
                    break;
            }

            return encryptedPwd;
        }



        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="sInputString"></param>
        /// <param name="sKey"></param>
        /// <returns></returns>
        public static string EncryptString(string sInputString, string sKey)
        {
            byte[] data = Encoding.UTF8.GetBytes(sInputString);
            DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            MemoryStream ms = new MemoryStream();     //创建其支持存储区为内存的流。 
            CryptoStream cs = new CryptoStream(ms, DES.CreateEncryptor(), CryptoStreamMode.Write);//将数据流连接到加密转换流 
            cs.Write(data, 0, data.Length);
            cs.FlushFinalBlock();     //用缓冲区的当前状态更新基础数据源或储存库，随后清除缓 
            StringBuilder ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }
            return ret.ToString();
        }


        /// <summary>
        /// DES解密字符串
        /// </summary>
        /// <param name="sInputString"></param>
        /// <param name="sKey"></param>
        /// <returns></returns>  
        public static string DecryptString(string sInputString, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            //Put     the     input     string     into     the     byte     array     
            byte[] inputByteArray = new byte[sInputString.Length / 2];
            string temp = "";
            int temp_len;
            for (int x = 0; x < sInputString.Length; x += 2)
            {
                temp = sInputString.Substring(x, 2);
                temp_len = temp.Length;

                int i = Convert.ToInt32(temp, 16);
                inputByteArray[x / 2] = (byte)i;
            }

            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            //Flush     the     data     through     the     crypto     stream     into     the     memory     stream     
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();

            //Get     the     decrypted     data     back     from     the     memory     stream     
            //建立StringBuild对象，CreateDecrypt使用的是流对象，必须把解密后的文本变成流对象     
            StringBuilder ret = new StringBuilder();

            return System.Text.Encoding.UTF8.GetString(ms.ToArray());
        }


    }//
}
